package com.ctbri.JWLetter.frame;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;

import com.ctbri.JWLetter.bean.Letter;
import com.ctbri.JWLetter.util.TypeUtils;

/**
 * 基础数据层实现
 * 
 * @author Hogan
 *
 */
public class BaseDao {
	
	@Resource
	protected ESDataPooledConnection eSDataPooledConnection;
	
	/**
	 * 获取指定类型的结果
	 * 
	 * @param map
	 * @param clazz
	 * @param key
	 * @return
	 */
	public <T> T get(String key, Class<T> clazz, Map<String, Object> map) {
		Object result = map.get(key);
		return TypeUtils.castType(result, clazz);
	}

	
	/**
	 * 根据提供的查询条件查询记录数
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public long findCountByOneParam(String key, Object value){
		TransportClient client = eSDataPooledConnection.getCurrentConnecton();
		long count = client.prepareSearch("articles")
				.setQuery(QueryBuilders.termQuery(key, value))
				.get()
				.getHits()
				.getTotalHits();
		eSDataPooledConnection.releaseConn(client);
		return count;
	}
	
	/**
	 * 根据提供的查询条件查询记录数
	 * 
	 * @param key
	 * @param values
	 * @return
	 */
	public long findCountByMultiParams(String key, Object... values){
		TransportClient client = eSDataPooledConnection.getCurrentConnecton();
		long count = client.prepareSearch("articles")
				.setQuery(QueryBuilders.termsQuery(key, values))
				.get()
				.getHits()
				.getTotalHits();
		eSDataPooledConnection.releaseConn(client);
		return count;
	}
	
	/**
	 * 
	 * @param startCount
	 * @param pageSize
	 * @param key
	 * @param value
	 * @return
	 */
	public List<Letter> findLettersByOneParam(int startCount, Integer pageSize, String key, Object value){
		TransportClient client = eSDataPooledConnection.getCurrentConnecton();
		SearchResponse sr = client.prepareSearch("articles")
				.setQuery(QueryBuilders.termQuery(key, value))
				.setFrom(startCount)
				.setSize(pageSize)
				.get();
		Iterator<SearchHit> it = sr.getHits().iterator();
		List<Letter> result = new ArrayList<Letter>();
		while (it.hasNext()) {
			Map<String, Object> map = ((SearchHit) it.next()).getSource();
			Letter letter = new Letter();
			letter.setLetterId(get("id", Integer.class, map));
			letter.setAuthor(get("author", String.class, map));
			letter.setCategory(get("categoryname", Integer.class, map));
			letter.setStatus(get("readornot", Integer.class, map));
			letter.setTitle(get("title", String.class, map));
			letter.setSubmitDate(get("submiteDatetime", Date.class, map));
			result.add(letter);
		}
		eSDataPooledConnection.releaseConn(client);
		return result;
	}
	
	/**
	 * 
	 * @param startCount
	 * @param pageSize
	 * @param key
	 * @param value
	 * @return
	 */
	public List<Letter> findLettersByMultiParams(int startCount, Integer pageSize, String key, Object... values){
		TransportClient client = eSDataPooledConnection.getCurrentConnecton();
		SearchResponse sr = client.prepareSearch("articles")
				.setQuery(QueryBuilders.termsQuery(key, values))
				.setFrom(startCount)
				.setSize(pageSize)
				.get();
		Iterator<SearchHit> it = sr.getHits().iterator();
		List<Letter> result = new ArrayList<Letter>();
		while (it.hasNext()) {
			Map<String, Object> map = ((SearchHit) it.next()).getSource();
			Letter letter = new Letter();
			letter.setLetterId(get("id", Integer.class, map));
			letter.setAuthor(get("author", String.class, map));
			letter.setCategory(get("categoryname", Integer.class, map));
			letter.setStatus(get("readornot", Integer.class, map));
			letter.setTitle(get("title", String.class, map));
			letter.setSubmitDate(get("submiteDatetime", Date.class, map));
			result.add(letter);
		}
		eSDataPooledConnection.releaseConn(client);
		return result;
	}
}
